<?php
class Mandrill {
    /**
     * @var MApplication
     */
    private static $app;

    /**
     * @return String : Current Mandrill version
     */
    public static function getVersion() {
        return '0.0.1';
    }

    /**
     * @return MApplication
     */
    public static function getApp() {
        return self::$app;
    }

    /**
     * @param MApplication $app
     */
    public static function setApp($app) {
        self::$app = $app;
    }

    /**
     * Create a new Mandrill application
     * @param String $config (optional) : Path to the config file
     * @return MApplication : Instance of Mandrill application
     */
    public static function createApplication($config = NULL) {
        return new MApplication($config);
    }
    
    public static function configureWithDbSettings(){
        ActiveRecord\Config::initialize(function($cfg){
            $cfg->set_model_directory(__DIR__.DS.'models');
            $cfg->set_connections(Mandrill::getApp()->dbConnection);
            $cfg->set_default_connection('mandrill');
        });
        $mandrillSettings = Setting::all(array('conditions'=>"name LIKE 'mandrill_%'"));
        foreach ($mandrillSettings as $setting) {
            $settingName = explode('_', $setting->name);
            if (count($settingName) <= 2){
                Mandrill::getApp()->{$settingName[1]} = $setting->value;
            }
        }
    }
    
    /**
     * Redirect the user to the Mandrill configuration process
     */
    public static function setupApplication() {
        MRequest::redirect(MApplication::SETUP_URL);
    }
    
    /**
     * Check if the Mandrill application is configured
     * @param String $config (optional) : Path to the config file
     * @return Boolean : Existence of the config file 
     */
    public static function checkSetup($config = NULL) {
        return (isset($config) && !empty($config)) ?  file_exists($config) : file_exists(MD_CONF_FILE);
    }
    
    /**
     * Start the Mandrill application
     */
    public static function start(){
        MLoader::getInstance()->enableAutoloading();
        MSession::getInstance()->start();
        if (self::checkSetup(MD_CONF_FILE)) {
            self::createApplication(MD_CONF_FILE);
            $tmp = MSession::getInstance()->getValue('mlSetup');
            if ($tmp !== false){
            self::configureWithDbSettings();
            }
            self::getApp()->run();
        } else {
            $router = MRouter::getInstance();
            $request = new MRequest();
            try {
                $router->route($request);
            } catch (Exception $exc) {
            }

            if ($request->getModule() != 'ml-setup'){
                self::setupApplication();
            } else {
                self::createApplication()->run();
            }
        }
    }

    /**
     * Returns the "Powered By Mandrill" text
     */
    public static function poweredBy() {
       return MTranslator::translate('mandrill.base.powered');
    }

    /**
     * @var Array : Class map for Mandrill core
     */
    public static $coreClasses = array(
        'MApplication'=>'/base/application.php',
        'MException'=>'/base/exception.php',
        'MLoader'=>'/base/loader.php',
        'MSingleton'=>'/base/singleton.php',
        'MModel'=>'/db/model.php',
        'MPackage'=>'/ext/package.php',
        'Menu'=>'/models/menu.php',
        'Module'=>'/models/module.php',
        'Module_Menu'=>'/models/module_menu.php',
        'Page'=>'/models/page.php',
        'Plugin'=>'/models/plugin.php',
        'Setting'=>'/models/setting.php',
        'User'=>'/models/user.php',
        'MFile'=>'/utils/file.php',
        'MString'=>'/utils/string.php',
        'MZip'=>'/utils/zip.php',
        'MNormalizer'=>'/utils/normalizer.php',
        'MTranslator'=>'/utils/translator.php',
        'MComponent'=>'/web/component.php',
        'MController'=>'/web/controller.php',
        'MDispatcher'=>'/web/dispatcher.php',
        'MError'=>'/web/error.php',
        'MMenu'=>'/web/menu.php',
        'MModule'=>'/web/module.php',
        'MPage'=>'/web/page.php',
        'MPlugin'=>'/web/plugin.php',
        'MPublisher'=>'/web/publisher.php',
        'MRequest'=>'/web/request.php',
        'MResponse'=>'/web/response.php',
        'MRouter'=>'/web/router-new.php',
        'MSession'=>'/web/session.php',
        'MSetting'=>'/web/setting.php',
        'MTemplate'=>'/web/template.php',
        'MView'=>'/web/view.php',
        'MWidget'=>'/web/widget.php',
        'MForm'=>'/web/form/form.php',
        'MFormCheckbox'=>'/web/form/formCheckbox.php',
        'MFormElement'=>'/web/form/formElement.php',
        'MFormFile'=>'/web/form/formFile.php',
        'MFormHidden'=>'/web/form/formHidden.php',
        'MFormImage'=>'/web/form/formImage.php',
        'MFormInput'=>'/web/form/formInput.php',
        'MFormPassword'=>'/web/form/formPassword.php',
        'MFormRadio'=>'/web/form/formRadio.php',
        'MFormReset'=>'/web/form/formReset.php',
        'MFormSelect'=>'/web/form/formSelect.php',
        'MFormSubmit'=>'/web/form/formSubmit.php',
        'MFormText'=>'/web/form/formText.php',
        'MFormTextarea'=>'/web/form/formTextarea.php',
        'MHtml'=>'/web/html/html.php',
        'MHtmlButtonColumn'=>'/web/html/htmlButtonColumn.php',
        'MHtmlColumn'=>'/web/html/htmlColumn.php',
        'MHtmlDataColumn'=>'/web/html/htmlDataColumn.php',
        'MHtmlElement'=>'/web/html/htmlElement.php',
        'MHtmlLinkPager'=>'/web/html/htmlLinkPager.php',
        'MHtmlTable'=>'/web/html/htmlTable.php',
    );

    /**
     * @var Array
     */
    public static $classes = array();
}

require 'utils/functions.php';
?>
